package com.example.soa_exp2.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.soa_exp2.Domain.Borrow;
import com.example.soa_exp2.Domain.Return;
import com.example.soa_exp2.Mapper.BorrowMapper;
import com.example.soa_exp2.Service.BorrowService;
import com.example.soa_exp2.Service.GoodsService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    private Borrow borrow;

    @Resource
    private BorrowMapper borrowMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    public List<Borrow> getBorrowList() {
        return borrowMapper.selectList(null);
    }

    @Override
    public Borrow getBorrowById(int id) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.eq("bor_rec_id", id);
        return borrowMapper.selectOne(wrapper);
    }

    @Override
    public List<Borrow> getBorrowByGoodsId(String id) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", id);
        return borrowMapper.selectList(wrapper);
    }

    @Override
    public List<Borrow> getBorrowByGoodsName(String name) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", name);
        return borrowMapper.selectList(wrapper);
    }

    @Override
    public List<Borrow> getBorrowByStuId(String stu_id) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu_id);
        return borrowMapper.selectList(wrapper);
    }

    @Override
    public List<Borrow> getBorrowByStuName(String stu_name) {
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.like("stu_name", stu_name);
        return borrowMapper.selectList(wrapper);
    }

    @Override
    public int addBorrow(Borrow borrow){
        if(goodsService.deleteGoodsStock(borrow.getGoods_id(),borrow.getBor_num()) != 0){
            return borrowMapper.insert(borrow);
        }else{
            return 0;
        }
    }

    @Override
    public int updateBorrowStatus(int id){
        UpdateWrapper<Borrow> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bor_rec_id",id);

        Borrow borrow = new Borrow();
        borrow.setStatus(true);

        return borrowMapper.update(borrow, updateWrapper);
    }

    @SneakyThrows
    @Override
    public List<Borrow> getBorrowByTime(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date_start = date + " 00:00:00";
        String date_end = date + " 23:59:59";
        Date date_min = sdf.parse(date_start);
        Date date_max = sdf.parse(date_end);
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.ge("bor_time", date_min).le("bor_time", date_max);
        return borrowMapper.selectList(wrapper);
    }

    @SneakyThrows
    @Override
    public List<Borrow> getBorrowByTime(String date_start, String date_end){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date_min = sdf.parse(date_start + " 00:00:00");
        Date date_max = sdf.parse(date_end + " 23:59:59");
        QueryWrapper<Borrow> wrapper = new QueryWrapper<>();
        wrapper.ge("bor_time", date_min).le("bor_time", date_max);
        return borrowMapper.selectList(wrapper);
    }
}
